﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Timers;

namespace Netfraction
{
    class Program
    {
        static RemoteMachine.Hub hub;

        enum HubsEnum : int
        {
            NextHub = 411,
            uHub = 1511,
            ADCH = 2780,
            DSHubPublicTestHub = 1111,
            Shaun=6110
        }

        private const string DSHUB_PUBLIC_IP = "89.33.154.169";
        private const string VMWARE_IP = "192.168.1.129";
        private const string VMWARE_PUBLIC_IP = "10.42.66.242";
        private const string SHAUN = "10.42.85.252";
        

        static void Main(string[] args)
        {
            /*DirectoryInfo d = new DirectoryInfo(@"D:\TV\");
            buckets = new Dictionary<int, int>();
            buckets.Add(175, 0);
            buckets.Add(233, 0);
            buckets.Add(350, 0);
            buckets.Add(700, 0);
            GetSizeBuckets(d);

            Console.WriteLine("175 MB Bucket: {0}", buckets[175]);
            Console.WriteLine("233 MB Bucket: {0}", buckets[233]);
            Console.WriteLine("350 MB Bucket: {0}", buckets[350]);
            Console.WriteLine("700 MB Bucket: {0}", buckets[700]);*/
            /*DirectoryInfo d = new DirectoryInfo(@"C:\");
            //GetSubs(d, 0);
            GetMaxFiles(d);
            Console.WriteLine("Max Files: {0}", maxFiles);
            //Console.WriteLine("Max Levels: {0}", maxDepth);*/

            
            // init this way instead of with new keyword coz it's a singleton class - do we even need to yet?
            LocalMachine.Client client = LocalMachine.Client.GetInstance();

            hub = new RemoteMachine.Hub();

            hub.Connection.Connect(new IPEndPoint(IPAddress.Parse(VMWARE_PUBLIC_IP), (int)HubsEnum.NextHub));
            
            
            /*ShareManager.ShareManager sManager = ShareManager.ShareManager.GetInstance();
            sManager.SetHashAlgorithm(Protocol.TIGR.Security.Cryptography.Tiger.Create());

            sManager.AddDirectory(new DirectoryInfo(@"D:\TV\"));*/
            Console.ReadLine();

            //hub.Connection.Disconnect();
        }
        /*
        static int maxDepth;
        static void GetSubs(DirectoryInfo d, int level)
        {
            try
            {
                DirectoryInfo[] subs = d.GetDirectories();
                if (subs.Length > 0)
                {
                    foreach (DirectoryInfo sub in subs)
                    {
                        GetSubs(sub, level + 1);
                    }
                }
                else
                {
                    if (level > maxDepth)
                    {
                        maxDepth = level;
                        Console.WriteLine(d.FullName);
                        Console.WriteLine("depth: {0}", level);
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                //Console.WriteLine("Error Accessing Directory (Unauthorized): {0}", d.FullName);
                return;
            }
            catch (DirectoryNotFoundException)
            {
                //Console.WriteLine("Error Accessing Directory (Not Found): {0}", d.FullName);
                return;
            }

        }

        static int maxFiles;
        static void GetMaxFiles(DirectoryInfo d)
        {
            int count = 0;
            try
            {
                count = d.GetFiles().Length;
                if (count > maxFiles)
                {
                    maxFiles = count;
                    Console.WriteLine(d.FullName);
                    Console.WriteLine("count: {0}", count);
                }
                if (count > 2000)
                {
                    Console.WriteLine(d.FullName);
                    Console.WriteLine("count: {0}", count);
                }
                foreach (DirectoryInfo sub in d.GetDirectories())
                {
                    GetMaxFiles(sub);
                }

            }
            catch (UnauthorizedAccessException)
            {
                //Console.WriteLine("Error Accessing Directory (Unauthorized): {0}", d.FullName);
                return;
            }
            catch (DirectoryNotFoundException)
            {
                //Console.WriteLine("Error Accessing Directory (Not Found): {0}", d.FullName);
                return;
            }

        }

        const long MEG_700_UPPER = 800 * 1024 * 1024;
        const long MEG_700_LOWER = 600 * 1024 * 1024;
        const long MEG_350_UPPER = 400 * 1024 * 1024;
        const long MEG_350_LOWER = 300 * 1024 * 1024;
        const long MEG_233_UPPER = 299 * 1024 * 1024;
        const long MEG_233_LOWER = 205 * 1024 * 1024;
        const long MEG_175_UPPER = 204 * 1024 * 1024;
        const long MEG_175_LOWER = 120 * 1024 * 1024;
        static Dictionary<int, int> buckets;
        static void GetSizeBuckets(DirectoryInfo d)
        {
            try
            {
                foreach (FileInfo fi in d.GetFiles())
                {
                    if (fi.Length >= MEG_175_LOWER && fi.Length <= MEG_175_UPPER)
                    {
                        buckets[175] = buckets[175] + 1;
                    }
                    else if (fi.Length >= MEG_233_LOWER && fi.Length <= MEG_233_UPPER)
                    {
                        buckets[233] = buckets[233] + 1;
                    }
                    else if (fi.Length >= MEG_350_LOWER && fi.Length <= MEG_350_UPPER)
                    {
                        buckets[350] = buckets[350] + 1;
                    }
                    else if (fi.Length >= MEG_700_LOWER && fi.Length <= MEG_700_UPPER)
                    {
                        buckets[700] = buckets[700] + 1;
                    }
                }
                foreach (DirectoryInfo sub in d.GetDirectories())
                {
                    GetSizeBuckets(sub);
                }

            }
            catch (UnauthorizedAccessException)
            {
                //Console.WriteLine("Error Accessing Directory (Unauthorized): {0}", d.FullName);
                return;
            }
            catch (DirectoryNotFoundException)
            {
                //Console.WriteLine("Error Accessing Directory (Not Found): {0}", d.FullName);
                return;
            }

        }*/
    }
}
